<template>
  <el-card v-loading="loading">
    <el-tabs v-model="activeName" :before-leave="beforeLeave">
      <el-tab-pane label="设置表基本信息" name="basic">
        <gen-table-basic ref="basic" @setActiveName="setActiveName"></gen-table-basic>
      </el-tab-pane>
      <el-tab-pane label="设置主表字段基本信息" name="parent">
        <gen-table-parent ref="parent" @setActiveName="setActiveName"></gen-table-parent>
      </el-tab-pane>
      <el-tab-pane label="设置子表字段基本信息" name="child">
        <gen-table-child ref="child" @setActiveName="setActiveName"></gen-table-child>
      </el-tab-pane>
      <el-tab-pane label="设置表生成配置" name="gene">
        <gen-table-gene ref="gene" @setActiveName="setActiveName"></gen-table-gene>
      </el-tab-pane>
    </el-tabs>
    <el-col :span="24" style="margin-top: 20px;margin-bottom: 20px">
      <el-col :span="2" :offset="6">
        <el-button @click="closeTab()">{{ $t('close') }}</el-button>
      </el-col>
      <el-col :span="2" :offset="2">
        <el-button v-if="this.activeName!=='basic'" type="primary" @click="back()">{{ $t('genTable.back') }}</el-button>
      </el-col>
      <el-col :span="2" :offset="2">
        <el-button v-if="this.activeName!=='gene'" type="primary" @click="next()">{{ $t('genTable.next') }}</el-button>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-button v-if="this.activeName==='gene'" type="primary" @click="override()">覆盖代码到项目</el-button>
      </el-col>
      <el-col :span="2" :offset="1">
        <el-button v-if="this.activeName==='gene'" type="primary" @click="download()">下载生成的代码</el-button>
      </el-col>
    </el-col>
  </el-card>
</template>

<script>
import mainContent from '@/views/main-content'
import GenTableBasic from './genTable-basic'
import GenTableParent from './genTable-parent'
import GenTableChild from './genTable-child'
import GenTableGene from './genTable-gene'
var content = mainContent
export default {

  data () {
    return {
      loading: false,
      dsCode: '',
      tableName: '',
      childTableCode: '',
      activeName: 'basic',
      handCheck: false,
      name: ''
    }
  },
  components: {
    GenTableBasic,
    GenTableParent,
    GenTableChild,
    GenTableGene
  },
  created () {
    this.setActiveName(this.$route.params.activeName, this.$route.params.dsCode,
      this.$route.params.tableName, this.$route.params.childTableCode)
  },
  methods: {
    beforeLeave (activeName, oldActiveName) {
      if (!this.handCheck) {
        this.$message({
          message: '不能通过选项卡切换',
          type: 'error',
          duration: 500,
          onClose: () => {
          }
        })
        return false
      } else {
        this.handCheck = false
      }
    },
    closeTab () {
      content.methods.tabRemoveHandle(this.$route.name)
    },
    next () {
      this.handCheck = true
      if (this.activeName === 'basic') {
        this.$refs.basic.dataFormSubmitHandle()
      } else if (this.activeName === 'parent') {
        this.$refs.parent.dataFormSubmitHandle()
      } else if (this.activeName === 'child') {
        this.$refs.child.dataFormSubmitHandle()
      }
    },
    back () {
      this.handCheck = true
      if (this.activeName === 'parent') {
        this.activeName = 'basic'
        this.$nextTick(() => {
          this.$refs.basic.dataForm.sourceCode = this.$refs.parent.dsCode
          this.$refs.basic.dataForm.tableName = this.$refs.parent.tableName
          this.$refs.basic.init()
        })
      } else if (this.activeName === 'child') {
        this.activeName = 'parent'
        this.$nextTick(() => {
          this.$refs.parent.dsCode = this.$refs.child.dsCode
          this.$refs.parent.tableName = this.$refs.child.tableName
          this.$refs.parent.init()
        })
      } else if (this.activeName === 'gene') {
        this.activeName = 'parent'
        this.$nextTick(() => {
          this.$refs.parent.dsCode = this.$refs.gene.dataForm.sourceCode
          this.$refs.parent.tableName = this.$refs.gene.dataForm.tableName
          this.$refs.parent.init()
        })
      }
    },
    setActiveName (name, dsCode, tableName, childTableCode) {
      this.dsCode = dsCode
      this.activeName = name
      this.childTableCode = childTableCode
      if (this.activeName === 'basic') {
        this.$nextTick(() => {
          this.$refs.basic.dataForm.sourceCode = dsCode
          this.$refs.basic.dataForm.tableName = tableName
          this.$refs.basic.init()
        })
      } else if (this.activeName === 'parent') {
        this.$nextTick(() => {
          this.$refs.parent.dsCode = dsCode
          this.$refs.parent.tableName = tableName
          this.$refs.parent.childTableCode = childTableCode
          this.$refs.parent.init()
        })
      } else if (this.activeName === 'child') {
        this.$nextTick(() => {
          this.$refs.child.dsCode = dsCode
          this.$refs.child.tableName = tableName
          this.$refs.child.childTableCode = childTableCode
          this.$refs.child.init()
        })
      } else if (this.activeName === 'gene') {
        this.$nextTick(() => {
          this.$refs.gene.dataForm.sourceCode = dsCode
          this.$refs.gene.dataForm.tableName = tableName
          this.$refs.gene.dataForm.childTableCode = childTableCode
          this.$refs.gene.init()
        })
      } else if (this.activeName === 'close') {
        this.closeTab()
      }
    },
    override () {
      this.$refs.gene.override()
    },
    download () {
      this.$refs.gene.download()
    }
  }
}
</script>
